import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QVBoxLayout, QWidget
from PyQt5.QtMultimedia import QMediaPlayer, QMediaContent
from PyQt5.QtMultimediaWidgets import QVideoWidget
from PyQt5.QtCore import QUrl
from pathlib import Path


class VideoPlayer(QMainWindow):
    def __init__(self):
        super().__init__()

        self.setWindowTitle("Video Player")

        # 创建视频窗口部件
        self.video_widget = QVideoWidget()

        # 创建媒体播放器
        self.player = QMediaPlayer(None, QMediaPlayer.VideoSurface)

        # 将视频输出设置为视频窗口部件
        self.player.setVideoOutput(self.video_widget)

        # 设置视频文件路径
        # url = "resource/example.mp4"  # 替换为你的视频文件路径
        url = QUrl.fromLocalFile(str(Path('../resource/fanhua.mp3').absolute()))
        content = QMediaContent(url)
        self.player.setMedia(content)

        # 播放视频
        self.player.play()

        # 创建一个布局并将视频窗口部件添加到其中
        layout = QVBoxLayout()
        layout.addWidget(self.video_widget)

        # 创建一个中央窗口小部件，并应用布局
        central_widget = QWidget()
        central_widget.setLayout(layout)
        self.setCentralWidget(central_widget)

if __name__ == "__main__":
    app = QApplication(sys.argv)

    player = VideoPlayer()
    player.show()

    sys.exit(app.exec_())